/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufrgs.inf.prav.interop.jsf;

import br.ufrgs.inf.prav.interop.AppConfig;
import br.ufrgs.inf.prav.interop.jsf.components.VideoComponent;
import br.ufrgs.inf.prav.interop.util.PathParser;
import java.security.acl.Group;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.component.UIComponent;

/**
 *
 * @author Fernando Arena Varella
 * @version 1.0
 */
public class VideoTag extends PravBaseTag {

    //attributes
    private String height, posterHref, width, scale;
    private boolean autoplay = false, autoHref = false, controller = true;
    private String mobileHref, tvdHref, webHref;
    private String scriptPath;


    public VideoTag(){

    }

    @Override
    protected void setProperties(UIComponent component) {
        /* you have to call the super class */
        super.setProperties(component);
        VideoComponent video = (VideoComponent) component;
        video.setAutoplay(autoplay);
        video.setAutoHref(autoHref);
        video.setController(controller);
        video.setHeight(height);
        video.setPosterHref(posterHref);
        video.setScale(scale);
        video.setWidth(width);


        overrideServerIP();
        video.setMobileHref(mobileHref);
        video.setTvdHref(tvdHref);
        video.setWebHref(webHref);

        video.setScriptPath(scriptPath);

    }

    private void overrideServerIP(){
        try {
            mobileHref = overrideIP(mobileHref);
            tvdHref = overrideIP(tvdHref);
            webHref = overrideIP(webHref);

        } catch (Exception e) {
            System.out.println("Erro em overrideServerIP: " + e.getMessage());
        }
    }

    /**
     * Override the ip of an URL to the default IP of the streaming server
     * Ex: If the IP of the local streaming server is "191.199.199.199" then the
     * URL "rtsp://255.255.255.0/movie.MP4" will be changed to "rtsp://199.199.199.199/movie.MP4"
     * @param url  the original url
     * @return      the fixed url
     */
    private String overrideIP(String url){
        if (PathParser.pathContainsProtocol(url, "rtsp")){
            Pattern pattern = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
            Matcher matcher = pattern.matcher(url);
            if (matcher.find()){
                String stIP = AppConfig.getInstance().getStreamingServerIP();
                url = url.replaceAll(matcher.group(), stIP);
            }
        }
        return url;
    }

    @Override
    public String getComponentType() {
        return VideoComponent.COMPONENT_TYPE;
    }

    @Override
    public String getRendererType() {
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc="Getters And Setters">
public void setAutoHref(boolean autoHref) {
        this.autoHref = autoHref;
    }

    public boolean isAutoplay() {
        return autoplay;
    }

    public void setAutoplay(boolean autoplay) {
        this.autoplay = autoplay;
    }

    public boolean isController() {
        return controller;
    }

    public void setController(boolean controller) {
        this.controller = controller;
    }


    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getPosterHref() {
        return posterHref;
    }

    public void setPosterHref(String posterHref) {
        this.posterHref = posterHref;
    }

    public String getMobileHref() {
        return mobileHref;
    }

    public void setMobileHref(String mobileHref) {
        this.mobileHref = mobileHref;
    }

    public String getTvdHref() {
        return tvdHref;
    }

    public void setTvdHref(String tvdHref) {
        this.tvdHref = tvdHref;
    }

    public String getWebHref() {
        return webHref;
    }

    public void setWebHref(String webHref) {
        this.webHref = webHref;
    }

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }


    public String getScale() {
        return scale;
    }

    public void setScale(String scale) {
        this.scale = scale;
    }

    public String getScriptPath() {
        return scriptPath;
    }

    public void setScriptPath(String scriptPath) {
        this.scriptPath = scriptPath;
    }


    // </editor-fold>

}
